<?php
Globals::requireClass('Controller');
Globals::requireModel('User');
Globals::requireTable('User');
Globals::requireTable('Vmoney');
Globals::requireTable('Vlevel');
Globals::requireTable('Msg');
Globals::requireTable('Circle');
Globals::requireTable('Circleimg');
Globals::requireTable('Circlelike');
Globals::requireTable('Circleorder');
Globals::requireTable('Circletypead');
Globals::requireTable('Circleheadnews');
Globals::requireTable('Circlecomm');
Globals::requireTable('Circlecollect');
Globals::requireTable('Circlebuytop');

class CircleController extends Controller
{
    protected $UserTable;
    protected $VmoneyTable;
    protected $VlevelTable;
    protected $MsgTable;
    protected $CircleTable;
    protected $CircleimgTable;
    protected $CirclelikeTable;
    protected $CircleorderTable;
    protected $CircletypeadTable;
    protected $CircleheadnewsTable;
    protected $CirclecommTable;
    protected $CirclecollectTable;
    protected $CirclebuytopTable;
	
	public static $defaultConfig = array(
		'viewEnabled'	=> true,
		'layoutEnabled'	=> true,
		'title'			=> null
	);
	
	public function __construct($config = null)
	{
		parent::__construct($config);
		$this->UserTable = new UserTable($config);
		$this->VmoneyTable = new VmoneyTable($config);
		$this->VlevelTable = new VlevelTable($config);
		$this->MsgTable = new MsgTable($config);
		$this->CircleTable = new CircleTable($config);
		$this->CircleimgTable = new CircleimgTable($config);
		$this->CirclelikeTable = new CirclelikeTable($config);
		$this->CircleorderTable = new CircleorderTable($config);
		$this->CircletypeadTable = new CircletypeadTable($config);
		$this->CircleheadnewsTable = new CircleheadnewsTable($config);
		$this->CirclecommTable = new CirclecommTable($config);
		$this->CirclecollectTable = new CirclecollectTable($config);
		$this->CirclebuytopTable = new CirclebuytopTable($config);
		dolog(json_encode($_GET));
	}
	
	public function indexAction()
	{
		$this->layout->pageTitle	= "test";
	}
	
	public function publishcircleAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>'-1','message'=>'用户信息不正确'));
	        exit();
	    }
	    
	    
	    $date = date('Y-m-d H:i:s');
	    $timetoday = strtotime(date('Y-m-d'));
	    
	    $circleid = $this->getIntParam('circleid');
	    
	    $content = $this->getParam('content');
	    $imgs = $this->getParam('imgs');
	    $link_img = $this->getParam('hyperlinkimage');
	    $link_content = $this->getParam('hyperlinkdescribe');
	    $link_url = $this->getParam('hyperlinkurl');
	    $assure_date = $this->getIntParam('assuredate');
	    if($assure_date < 1){
	        echo json_encode(array('returncode'=>'-4','message'=>'圈子担保期不正确'));
	        exit();
	    }
	    $price = $this->getParam('price');
	    if($price <= 0){
	        echo json_encode(array('returncode'=>'-3','message'=>'价格不正确'));
	        exit();
	    }
	    $is_fixation = $this->getIntParam('isfixation');
	    $remark = $this->getParam('remark');
	    $repertory = $this->getIntParam('repertory');
	    
	    if($circleid < 1){
	        $user_money = $this->VmoneyTable->getUserVmoneyCount($uid);
	        if($user_money < 8){
	            echo json_encode(array('returncode'=>'-2','message'=>'您的账户V币余额要大于等于8V币作为担保哦！'));
	            exit();
	        }
	        
	        $data = array(
	            'uid' => $uid,
	            'content' => $content,
	            'link_img' => $link_img,
	            'link_content' => $link_content,
	            'link_url' => $link_url,
	            'assure_date' => $assure_date,
	            'price' => $price,
	            'is_fixation' => $is_fixation,
	            'remark' => $remark,
	            'create_date' => $date,
	            'update_date' => date('Y-m-d',$timetoday + $assure_date * 86400),
	            'img_count' => is_array($imgs) ? count($imgs) : 0,
	            'repertory' => $repertory
	        );
	        
	        $cid = $this->CircleTable->add($data,true);
	        
	        if(is_array($imgs)){
	            foreach ($imgs as $v){
	                $imgs_data = array(
	                    'uid' => $uid,
	                    'cid' => $cid,
	                    'img_url' => $v,
	                    'create_date' => $date
	                );
	                $this->CircleimgTable->add($imgs_data);
	            }
	        }
	        
	        $vmoney_data = array(
	            'uid' => $uid,
	            'money' => '-0.1',
	            'type' => 5,
	            'remark' => '发布圈子扣除v币，圈子id'.$cid
	        );
	        $this->VmoneyTable->add($vmoney_data);
	        
	    }else{
	        $circle_data = $this->CircleTable->getRow($circleid);
	        if($circle_data['uid'] != $uid){
	            echo json_encode(array('returncode'=>'-5','message'=>'不能修改他人圈子'));
	            exit();
	        }
	        
	        $data = array(
	            'content' => $content,
	            'link_img' => $link_img,
	            'link_content' => $link_content,
	            'link_url' => $link_url,
	            'remark' => $remark,
	            'create_date' => $date,
	            'update_date' => date('Y-m-d',$timetoday + $assure_date * 86400),
	            'repertory' => $repertory,
	            'assure_date' => $assure_date,
	            'price' => $price,
	            'is_fixation' => $is_fixation
	        );
	        $this->CircleTable->update($data,$circle_data['id']);
	        
	        $this->CircleimgTable->update(array('status'=>0),array('cid'=>$circle_data['id']));
	        if(is_array($imgs)){
	            foreach ($imgs as $v){
	                $imgs_data = array(
	                    'uid' => $uid,
	                    'cid' => $circle_data['id'],
	                    'img_url' => $v,
	                    'create_date' => $date
	                );
	                $this->CircleimgTable->add($imgs_data);
	            }
	        }
	        
	    }
	    
	    echo json_encode(array('returncode'=>'0','message'=>'小树林圈子因您而变得更美好'));
	    exit();
	}
	
	public function circlelistAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>'-1','message'=>'用户信息不正确','data'=>array()));
	        exit();
	    }
	    
	    $date = date('Y-m-d H:i:s');
	    $time = time();
	    $timetoday = strtotime(date('Y-m-d'));
	    
	    $type = $this->getIntParam('circlelisttype');
	    $type = ($type < 0 || $type > 4) ? 0 : $type;
	    $circle_uid = $this->getIntParam('circleuid');
	    $page = $this->getIntParam('page');
	    $page = $page < 1 ? 1 : $page;
	    $size = $this->getIntParam('size');
	    $size = $size < 1 ? 10 : $size;
	    
	    $user_data = array();
	    
	    $data = array();
	    if($type == 0 || $type == 1){
	        $tmp_data = array();
    	    $cids = array();
    	    $where = "status = 1 and end_date > '{$date}'";
    	    $order = " id desc ";
    	    $re = $this->CirclebuytopTable->listPage($where,$order,$size,$page,'id desc');
    	    if(count($re) > 0){
    	        $circle_ids = array();
    	        foreach ($re as $v){
    	            $circle_ids[] = $v['cid'];
    	        }
    	        $circle_id = implode(',', $circle_ids);
    	        $re = $this->CircleTable->listAll("status = 1 and is_order = 0 and id in ({$circle_id})");
    	        foreach ($re as $v){
    	            $tmp_like_uid = array();
    	            $tmp_like_list = $this->CirclelikeTable->listAll(array('cid'=>$v['id'],'status'=>1));
    	            foreach ($tmp_like_list as $v2){
    	                $tmp_like_uid[] = $v2['uid'];
    	            }
    	            $tmp_like_uid = array_reverse($tmp_like_uid);
    	            
    	            $tmp_comm_uid = array();
    	            $tmp_comm_list = $this->CirclecommTable->listAll(array('cid'=>$v['id'],'status'=>1));
    	            foreach ($tmp_comm_list as $v2){
    	                $tmp_comm_uid[] = $v2['uid'];
    	            }
    	            
    	            if((int)$v['is_fixation'] == 0){
    	                $new_v = $this->getCirclePrice($v['create_date'],$timetoday,$v['price'],$v['assure_date']);
    	                $v['price'] = $new_v['price'];
    	                $v['assuredate'] = $new_v['day'];
    	            }
    	            
    	            $is_like = in_array($uid, $tmp_like_uid) ? 1 : 0;
    	            $tmp_collect = $this->CirclecollectTable->getRowWithFields('id',array('cid'=>$v['id'],'uid'=>$uid,'status'=>1));
    	            $is_collect = isset($tmp_collect['id']) ? 1 : 0;
    	            
    	            if(!in_array($v['uid'], $user_data)){
    	                $tmp_user = $this->UserTable->getRowWithFields('id,headurl',$v['uid']);
    	                $user_data[$tmp_user['id']] = $tmp_user;
    	            }
    	            $tmp_headurl = $user_data[$v['uid']]['headurl'];
    	            
    	            if($v['uid'] == $uid){
    	                $v['remark'] = $v['remark'];
    	                $v['isfixation'] = $v['is_fixation'];
    	            }
    	            
    	            $v['order_count'] = $this->CircleorderTable->listCount(array('old_cid'=>$v['id'],'status'=>2));
    	            $v['repertory'] = $v['repertory'];
    	            
    	            $tmp_data[$v['id']] = array(
    	                'circleuid' => (string)$v['uid'],
    	                'circleid' => (string)$v['id'],
    	                'avatar' => $tmp_headurl,
    	                'name' => ($v['uid'] == 1374 ? '小树林APP官方' : '用户ID：'.$v['uid']),
    	                'content' => (string)$v['content'],
    	                'imgs' => array(),
    	                'thumbnails' => array(),
    	                'hyperlinkimage' => (string)$v['link_img'],
    	                'hyperlinkdescribe' => (string)$v['link_content'],
    	                'hyperlinkurl' => (string)$v['link_url'],
    	                'assuredate' => (string)$v['assure_date'],
    	                'price' => (string)round($v['price'],2),
    	                'date' => (string)$v['create_date'],
    	                'iscollect' => (string)$is_collect,
    	                'isstick' => (string)1,
    	                'islike' => (string)$is_like,
    	                'likelist' => $tmp_like_uid,
    	                'commentlist' => $tmp_comm_uid,
    	                'order_count' => (string)$v['order_count'],
    	                'repertory' => (string)$v['repertory']
    	            );
    	            
    	            $cids[] = $v['id'];
    	        }
    	        foreach ($circle_ids as $v){
    	            $data[$v] = $tmp_data[$v];
    	        }
    	    }
	    }
	    
	    
	    
	    
	    $where = " status = 1 ";
	    
	    if(isset($circle_id) && !empty($circle_id)){
	        $where .= " and id not in ({$circle_id}) ";
	    }
	    
	    if($circle_uid > 0){
	        $where .= " and uid = {$circle_uid} ";
	    }
	    
	    
	    $is_order = " and is_order = 0 ";
	    $order = " id desc ";
	    switch ($type){
	        case 1:
	            $order = " like_count desc ";
	            break;
	        case 2:
	            $like_cids = $this->CirclecollectTable->getUserCollect($uid);
	            if(count($like_cids) < 1){
	                $where .= " and 1 != 1 ";
	            }else{
	                $like_cids = array_reverse($like_cids);
	                $like_cids_str = implode(',', $like_cids);
	                $where .= " and id in ({$like_cids_str}) ";
	            }
	            break;
	        case 3:
	            $is_order = " and is_order = 1 ";
	            
	            $order_cids = $this->CircleorderTable->getUserOrder($uid);
	            if(count($order_cids) < 1){
	                $where .= " and 1 != 1 ";
	            }else{
	                $order_cids_str = implode(',', $order_cids);
	                $where .= " and id in ({$order_cids_str}) ";
	            }
	            break;
	        case 4:
	            $where .= " and uid = {$uid} ";
	            break;
	        default:
	            break;
	    }
	    
	    $where = $where.$is_order;
	    
	    $size_surplus = $size - count($data);
	    $re = $this->CircleTable->listPage($where,$order,$size_surplus,$page);
	    
	    foreach ($re as $v){
	        $tmp_like_uid = array();
	        $tmp_like_list = $this->CirclelikeTable->listAll(array('cid'=>$v['id'],'status'=>1));
	        foreach ($tmp_like_list as $v2){
	            $tmp_like_uid[] = $v2['uid'];
	        }
	        $tmp_like_uid = array_reverse($tmp_like_uid);
	        
	        $tmp_comm_uid = array();
	        $tmp_comm_list = $this->CirclecommTable->listAll(array('cid'=>$v['id'],'status'=>1));
	        foreach ($tmp_comm_list as $v2){
	            $tmp_comm_uid[] = $v2['uid'];
	        }
	        
	        if((int)$v['is_fixation'] == 0 && $v['is_order'] == 0){
	            $new_v = $this->getCirclePrice($v['create_date'],$timetoday,$v['price'],$v['assure_date']);
	            $v['price'] = $new_v['price'];
	            $v['assure_date'] = $new_v['day'];
	        }elseif($v['is_order'] == 1){
	            $new_v = $this->getCirclePrice($v['create_date'],$timetoday,$v['price'],$v['assure_date']);
	            $v['assure_date'] = $new_v['day'];
	        }
	        
	        $is_like = in_array($uid, $tmp_like_uid) ? 1 : 0;
	        $tmp_collect = $this->CirclecollectTable->getRowWithFields('id',array('cid'=>$v['id'],'uid'=>$uid,'status'=>1));
	        $is_collect = isset($tmp_collect['id']) ? 1 : 0;
	        
	        if(!in_array($v['uid'], $user_data)){
	            $tmp_user = $this->UserTable->getRowWithFields('id,headurl',$v['uid']);
	            $user_data[$tmp_user['id']] = $tmp_user;
	        }
	        $tmp_headurl = $user_data[$v['uid']]['headurl'];
	        
	        if($v['uid'] == $uid){
	            $v['remark'] = $v['remark'];
	            $v['isfixation'] = $v['is_fixation'];
	        }
	        
	        if($type != 3){
	            $v['order_count'] = $this->CircleorderTable->listCount(array('old_cid'=>$v['id'],'status'=>2));
	            $v['repertory'] = $v['repertory'];
	        }
	        
	        $data[$v['id']] = array(
	            'circleuid' => (string)$v['uid'],
	            'circleid' => (string)$v['id'],
	            'avatar' => $tmp_headurl,
	            'name' => ($v['uid'] == 1374 ? '小树林APP官方' : '用户ID：'.$v['uid']),
	            'content' => (string)$v['content'],
	            'imgs' => array(),
	            'thumbnails' => array(),
	            'hyperlinkimage' => (string)$v['link_img'],
	            'hyperlinkdescribe' => (string)$v['link_content'],
	            'hyperlinkurl' => (string)$v['link_url'],
	            'assuredate' => (string)$v['assure_date'],
	            'price' => (string)round($v['price'],2),
	            'date' => (string)$v['create_date'],
	            'iscollect' => (string)$is_collect,
	            'isstick' => (string)0,
	            'islike' => (string)$is_like,
	            'likelist' => $tmp_like_uid,
	            'commentlist' => $tmp_comm_uid,
	            'order_count' => (string)$v['order_count'],
	            'repertory' => (string)$v['repertory']
	        );
	        
	        $cids[] = $v['id'];
	    }
	    
	    
	    if(count($cids) > 0){
	        $cids_str = implode(',', $cids);
	        $img_where = " cid in ({$cids_str}) and status = 1 ";
	        $img_data = $this->CircleimgTable->listAll($img_where);
	        foreach ($img_data as $v){
	            $data[$v['cid']]['imgs'][] = $v['img_url'];
	            $data[$v['cid']]['thumbnails'][] = $v['img_url'];
	        }
	    }
	    
	    
	    $re_data = array();
	    if($type == 2){
	        foreach ($like_cids as $v){
	            if(isset($data[$v])){
	               $re_data[] = $data[$v];
	            }
	        }
	    }else{
	        foreach ($data as $v){
	            $re_data[] = $v;
	        }
	    }
	    
	    
	    echo json_encode(array('returncode'=>'0','message'=>'','data'=>$re_data));
	    exit();
	}
	
	protected function getCirclePrice($createdate,$nowtime,$price,$day){
	    $createtime = strtotime($createdate);
	    if($createtime > $nowtime){
	        $re = array(
	            'price' => $price,
	            'day' => $day
	        );
	        return $re;
	    }
	    $tmptime = $nowtime - $createtime;
	    $gone_day = (int)$tmptime / 86400;
	    $last_day = $day - (int)$gone_day - 1;
	    $last_day = $last_day < 0 ? 0 : $last_day;
	    $re = array(
	        'price' => round($price / $day * $last_day,2),
	        'day' => $last_day
	    );
	    
	    if($re['day'] > 0 && $re['price'] == 0){
	        $re['price'] = 0.01;
	    }
	    
	    return $re;
	}
	
	public function typeadAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>'-1','message'=>'用户信息不正确','data'=>array()));
	        exit();
	    }
	    
	    $type = $this->getIntParam('circlelisttype');
	    $where = array(
	        'status' => 1,
	        'type' => $type
	    );
	    $data = $this->CircletypeadTable->listAll($where);
	    
	    $data_re = array();
	    foreach ($data as $v){
	        $data_re[] = array(
	            'generalizeimage' => DOMAIN.str_replace('../', '', $v['img_url']),
	            'generalizeurl' => $v['link']
	        );
	    }
	    
	    echo json_encode(array('returncode'=>'0','message'=>'','data'=>$data_re));
	    exit();
	}
	
	public function dolikeAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>'-1','message'=>'用户信息不正确'));
	        exit();
	    }
	    
	    
	    $circleid = $this->getIntParam('circleid');
	    $islike = $this->getIntParam('islike');
	    
	    $circledata = $this->CircleTable->getRow($circleid);
	    if(!isset($circledata['id']) || $circledata['id'] < 1 || $circledata['status'] != 1){
	        echo json_encode(array('returncode'=>'-2','message'=>'无此圈子'));
	        exit();
	    }
	    
	    if($islike == 1){
	        $hl = $this->CirclelikeTable->getRow(array('uid'=>$uid,'cid'=>$circleid,'status'=>1));
	        if(isset($hl['id']) && $hl['id'] > 0){
	            echo json_encode(array('returncode'=>'-3','message'=>'已经赞过'));
	            exit();
	        }else{
	            $this->CirclelikeTable->add(array(
	                'uid' => $uid,
	                'cid' => $circleid,
	                'create_date' => date('Y-m-d H:i:s')
	            ));
	            $this->CircleTable->update(array('like_count'=>$circledata['like_count']+1),$circledata['id']);
	            echo json_encode(array('returncode'=>'0','message'=>'多一位知己，多一份温暖！'));
	            exit();
	        }
	    }else{
	        $hl = $this->CirclelikeTable->getRow(array('uid'=>$uid,'cid'=>$circleid,'status'=>1));
	        if(!isset($hl['id']) || $hl['id'] < 1){
	            echo json_encode(array('returncode'=>'-4','message'=>'没有赞过'));
	            exit();
	        }else{
	            $this->CirclelikeTable->update(array('status'=>0),$hl['id']);
	            $this->CircleTable->update(array('like_count'=>$circledata['like_count']-1),$circledata['id']);
	            echo json_encode(array('returncode'=>'0','message'=>'路途漫漫，只能一人前行！'));
	            exit();
	        }
	    }
	    
	}
	
	
	public function docommAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>'-1','message'=>'用户信息不正确'));
	        exit();
	    }
	    
	    
	    $circleid = $this->getIntParam('circleid');
	    $content = $this->getIntParam('content');
	    
	    $circledata = $this->CircleTable->getRow($circleid);
	    if(!isset($circledata['id']) || $circledata['id'] < 1 || $circledata['status'] != 1){
	        echo json_encode(array('returncode'=>'-2','message'=>'无此圈子'));
	        exit();
	    }
	    
	    $this->CirclecommTable->add(array(
	        'cid' => $circleid,
	        'uid' => $uid,
	        'create_date' => date('Y-m-d H:i:s'),
	        'content' => $content
	    ));
	    
	    $this->CircleTable->update(array('comm_count'=>$circledata['comm_count']+1),$circledata['id']);
	    
	    echo json_encode(array('returncode'=>'0','message'=>'操作成功'));
	    exit();
	    
	}
	
	public function docollectAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>'-1','message'=>'用户信息不正确'));
	        exit();
	    }
	    
	    
	    $circleid = $this->getIntParam('circleid');
	    $iscollect = $this->getIntParam('iscollect');
	    
	    $circledata = $this->CircleTable->getRow($circleid);
	    if(!isset($circledata['id']) || $circledata['id'] < 1 || $circledata['status'] != 1){
	        echo json_encode(array('returncode'=>'-2','message'=>'无此圈子'));
	        exit();
	    }
	    
	    if($iscollect == 1){
	        $hl = $this->CirclecollectTable->getRow(array('uid'=>$uid,'cid'=>$circleid,'status'=>1));
	        if(isset($hl['id']) && $hl['id'] > 0){
	            echo json_encode(array('returncode'=>'-3','message'=>'已经收藏'));
	            exit();
	        }else{
	            $this->CirclecollectTable->add(array(
	                'uid' => $uid,
	                'cid' => $circleid,
	                'create_date' => date('Y-m-d H:i:s')
	            ));
	            $this->CircleTable->update(array('collect_count'=>$circledata['collect_count']+1),$circledata['id']);
	            echo json_encode(array('returncode'=>'0','message'=>'我有一个小秘密！就不告诉你！'));
	            exit();
	        }
	    }else{
	        $hl = $this->CirclecollectTable->getRow(array('uid'=>$uid,'cid'=>$circleid,'status'=>1));
	        if(!isset($hl['id']) || $hl['id'] < 1){
	            echo json_encode(array('returncode'=>'-4','message'=>'没有收藏'));
	            exit();
	        }else{
	            $this->CirclecollectTable->update(array('status'=>0),$hl['id']);
	            $this->CircleTable->update(array('collect_count'=>$circledata['collect_count']-1),$circledata['id']);
	            echo json_encode(array('returncode'=>'0','message'=>'人生记忆终为空！'));
	            exit();
	        }
	    }
	    
	    
	    
	}
	
	
	public function buytopAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>'-1','message'=>'用户信息不正确'));
	        exit();
	    }
	    
	    $circleid = $this->getIntParam('circleid');
	    $sticktime = $this->getIntParam('sticktime');
	    
	    $circledata = $this->CircleTable->getRow($circleid);
	    if(!isset($circledata['id']) || $circledata['id'] < 1 || $circledata['status'] != 1){
	        echo json_encode(array('returncode'=>'-2','message'=>'无此圈子'));
	        exit();
	    }
	    
	    $date = date('Y-m-d H:i:s');
	    $time = time();
	    $price = 0.1 * $sticktime + 1 / $sticktime;
	    
	    $usermoney = $this->VmoneyTable->getUserVmoneyCount($uid);
	    if($usermoney < $price){
	        echo json_encode(array('returncode'=>'-3','message'=>'v币不足'));
	        exit();
	    }
	    
	    $is_buy = $this->CirclebuytopTable->getRow("end_date > '{$date}' and cid = {$circleid} and status = 1",'id desc');
	    if(isset($is_buy['id']) && $is_buy['id'] > 0){
	        $start_date = $is_buy['end_date'];
	    }else{
	        $start_date = $date;
	    }
	    $end_date = date('Y-m-d H:i:s',strtotime($start_date)+$sticktime*86400);
	    
	    $data = array(
	        'cid' => $circleid,
	        'uid' => $uid,
	        'status' => 1,
	        'create_date' => $date,
	        'buyday' => $sticktime,
	        'price' => $price,
	        'start_date' => $start_date,
	        'end_date' => $end_date
	    );
	    
	    $this->CirclebuytopTable->add($data);
	    
	    $vmoney_data = array(
	        'uid' => $uid,
	        'money' => 0-$price,
	        'type' => 6,
	        'remark' => '置顶圈子扣除v币，圈子id'.$circleid
	    );
	    $this->VmoneyTable->add($vmoney_data);
	    
	    
	    echo json_encode(array('returncode'=>'0','message'=>'操作成功'));
	    exit();
	}
	
	public function deleteAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>'-1','message'=>'用户信息不正确'));
	        exit();
	    }
	    
	    $circleid = $this->getIntParam('circleid');
	    $circleuid = $this->getIntParam('circleuid');
	    
	    $circledata = $this->CircleTable->getRow($circleid);
	    if(!isset($circledata['id']) || $circledata['id'] < 1 || $circledata['status'] != 1){
	        echo json_encode(array('returncode'=>'-2','message'=>'无此圈子'));
	        exit();
	    }
	    
	    if($uid != 1374){
	        if($circledata['uid'] != $uid){
	            echo json_encode(array('returncode'=>'-3','message'=>'不能删除他人圈子'));
	            exit();
	        }
	    }
	    
	    $this->CircleTable->update(array('status'=>0),$circledata['id']);
	    if($uid == 1374){
	        $vmoney_data = array(
	            'uid' => $circleuid,
	            'money' => -1,
	            'type' => 7,
	            'remark' => '圈子违规扣除v币，圈子id'.$circledata['id']
	        );
	        $this->VmoneyTable->add($vmoney_data);
	        
	        $content = mb_substr($circledata['content'], 0, 90);
	        $exreas = array(
	            'content' => "您发布的圈子：（{$content}）\n违反了小树林发布圈子内容规定，现做删除处理，并扣除1V币的处罚，如有任何疑问请点击去反馈按钮找客服反馈。",
	            'pushtype' => 16
	        );
	        do_jpush($circleuid,'违规圈子被删除处罚通知! ',$exreas);
	    }
	    
	    
	    echo json_encode(array('returncode'=>'0','message'=>'操作成功'));
	    exit();
	}
	
	public function createorderAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    $user = $this->UserTable->getRow($uid);
	    if($uid < 1 || $user['id'] < 1){
	        echo json_encode(array('returncode'=>'-1','message'=>'用户信息不正确','orderid'=>'','price'=>'0.00'));
	        exit();
	    }
	    
	    $id = $this->getIntParam('commodityid');
	    $type = $this->getIntParam('commoditytype');
	    $paytype = $this->getIntParam('paytype');
	    $extendjson = $this->getParam('extendjson');
	    
	    $date = date('Y-m-d H:i:s');
	    $time = time();
	    $timetoday = strtotime(date('Y-m-d'));
	    
	    $circle_data = $this->CircleTable->getRow($id);
	    if(!isset($circle_data['id']) || $circle_data['status'] == 0){
	        echo json_encode(array('returncode'=>'-2','message'=>'无此圈子','orderid'=>'','price'=>'0.00'));
	        exit();
	    }
	    
	    if($type == 1){
	        $price = $circle_data['price'];
	        if($circle_data['is_fixation'] == 0){
	            $new_v = $this->getCirclePrice($circle_data['create_date'], $timetoday, $circle_data['price'], $circle_data['assure_date']);
	            $price = $new_v['price'];
	            $end_day = $new_v['day'];
	        }else{
	            $end_day = $circle_data['assure_date'];
	        }
	        
	        $hb = $this->CircleorderTable->listCount(array('old_cid'=>$circle_data['id'],'status'=>2));
	        if($hb >= $circle_data['repertory']){
	            echo json_encode(array('returncode'=>'-2','message'=>'此圈子已抢光，您可以看看其他圈子。','orderid'=>'','price'=>'0.00'));
	            exit();
	        }
	        
	        $message = '爱生活！爱自己！';
	    }elseif($type == 2){
	        $json_arr = json_decode($extendjson,true);
	        $sticktime = $json_arr['sticktime'];
	        if((int)$sticktime == 0){
	            echo json_encode(array('returncode'=>'-3','message'=>'置顶天数错误','orderid'=>'','price'=>'0.00'));
	            exit();
	        }
	        $price = 0.1 * $sticktime + 0.1 - $sticktime / 3650;
	        $price = round($price,2);
	        
	        $message = '圈子已置顶，会被更多人看到。';
	    }else{
	        echo json_encode(array('returncode'=>'-4','message'=>'commoditytype参数错误','orderid'=>'','price'=>'0.00'));
	        exit();
	    }
	    
	    
	    if($paytype == 5){
	        $user_vmoney = $this->VmoneyTable->getUserVmoneyCount($uid);
	        if($user_vmoney < $price){
	            echo json_encode(array('returncode'=>'-5','message'=>'v币不足','orderid'=>'','price'=>'0.00'));
	            exit();
	        }
	    }
	    
	    
	    if($type == 1){
    	    $db = $this->CircleTable->getDatabase();
    	    $sql = "insert into tb_circle (uid,content,img_count,link_img,link_content,link_url,assure_date,price,is_fixation,remark,status,create_date,update_date,rank,like_count,comm_count,collect_count,is_order,repertory) (select uid,content,img_count,link_img,link_content,link_url,assure_date,price,is_fixation,remark,status,'{$date}',update_date,rank,like_count,comm_count,collect_count,1,repertory from tb_circle where id = {$id})";
    	    $is_true = $db->query($sql);
    	    if(!$is_true){
    	        echo json_encode(array('returncode'=>'-6','message'=>'系统错误','orderid'=>'','price'=>'0.00'));
    	        exit();
    	    }
    	    $newid = $db->getInsertId();
    	    $sql = "insert into tb_circle_collect (uid,cid,status,create_date,is_order) (select uid,{$newid},2,create_date,1 from tb_circle_collect where cid = {$id})";
    	    $db->query($sql);
    	    $sql = "insert into tb_circle_comm (uid,cid,status,create_date,content,is_order) (select uid,{$newid},status,create_date,content,1 from tb_circle_comm where cid = {$id})";
    	    $db->query($sql);
    	    $sql = "insert into tb_circle_img (uid,cid,img_url,status,create_date,rank,is_order) (select uid,{$newid},img_url,status,create_date,rank,1 from tb_circle_img where cid = {$id})";
    	    $db->query($sql);
    	    $sql = "insert into tb_circle_like (uid,cid,status,create_date,is_order) (select uid,{$newid},status,create_date,1 from tb_circle_like where cid = {$id})";
    	    $db->query($sql);
    	    
    	    $this->CircleTable->update(array(
    	        'fixation_date' => $date,
    	        'fixation_end_date' => date('Y-m-d',$timetoday + $end_day * 86400)
    	    ),$newid);
    	    
	    }else{
	        $newid = $id;
	    }
	    
	    $data = array(
	        'orderid' => substr($time, -9) . mt_rand(10, 99),
	        'uid' => $uid,
	        'cid' => $newid,
	        'create_date' => $date,
	        'type' => $type,
	        'paytype' => $paytype,
	        'pay_day' => $type == 2 ? $sticktime : 0,
	        'price' => $price,
	        'old_cid' => $id
	    );
	    $this->CircleorderTable->add($data);
	    
	    if($paytype == 5){
	        $this->redirectToAction('payok',array('orderid'=>$data['orderid'],'paytype'=>5));
	        exit();
	    }
	    
	    $title = mb_substr($circle_data['content'], 0, 90);
	    echo json_encode(array('returncode'=>'0','message'=>$message,'orderid'=>(string)$data['orderid'],'price'=>(string)$price,'title'=>$title,'paytype'=>(string)$paytype));
	    exit();
	}
	
	public function payokAction(){
	    $orderid = $this->getParam('orderid');
	    $pay_type = $this->getIntParam('paytype');
	    $pay_orderid = $this->getParam('pay_orderid');
	    
	    $date = date('Y-m-d H:i:s');
	    
	    $order_data = $this->CircleorderTable->getRow(array('orderid'=>$orderid));
	    if(!isset($order_data['id']) || $order_data['status'] != 1){
	        echo json_encode(array('returncode'=>'-1','message'=>'无此订单'));
	        exit();
	    }
	    
	    $user_data = $this->UserTable->getRow($order_data['uid']);
	    $circle_data = $this->CircleTable->getRow($order_data['cid']);
	    if($order_data['type'] == 1){
	        $circle_old_data = $this->CircleTable->getRow($order_data['old_cid']);
	    }
	    
	    if($pay_type == 5){
	        $user_vmoney = $this->VmoneyTable->getUserVmoneyCount($user_data['id']);
	        if($user_vmoney < $order_data['price']){
	            echo json_encode(array('returncode'=>'-5','message'=>'v币不足','orderid'=>'','price'=>'0.00'));
	            exit();
	        }
	        
	        $vmoney_data['uid'] = $user_data['id'];
	        $vmoney_data['type'] = 6;
	        $vmoney_data['money'] = (0-$order_data['price']);
	        if($order_data['type'] == 2){
	            $vmoney_data['remark'] = '购买圈子置顶消耗V币'.$order_data['price'];
	        }else{
	            $vmoney_data['remark'] = '购买圈子商品消耗V币'.$order_data['price'];
	        }
	        $this->VmoneyTable->add($vmoney_data);
	    }
	    
	    
	    $this->CircleorderTable->update(array(
	        'status' => 2,
	        'pay_date' => $date,
	        'pay_orderid' => $pay_orderid
	    ),$order_data['id']);
	    
	    $this->CircleTable->update(array(
	        'status'=>1
	    ),$circle_data['id']);
	    
	    if($order_data['type'] == 1){
	        $message = '圈子商品购买成功';
	        
	        $get_money = round($order_data['price'] * 0.02,2);
	        $get_money = $get_money < 0.01 ? 0.01 : $get_money;
	        
	        $vmoney_data['uid'] = $order_data['uid'];
	        $vmoney_data['type'] = 8;
	        $vmoney_data['money'] = $get_money;
	        $vmoney_data['remark'] = '购买圈子商品获得V币'.$get_money;
	        $this->VmoneyTable->add($vmoney_data);
	        
	        $vlevel_data['uid'] = $order_data['uid'];
	        $vlevel_data['type'] = 6;
	        $vlevel_data['level'] = $get_money;
	        $vlevel_data['remark'] = '购买圈子商品获得V级'.$get_money;
	        $this->VlevelTable->add($vlevel_data);
	        
	        $get_money = round($order_data['price'] * 0.01,2);
	        $get_money = $get_money < 0.01 ? 0.01 : $get_money;
	        
	        $vmoney_data['uid'] = $circle_data['uid'];
	        $vmoney_data['type'] = 9;
	        $vmoney_data['money'] = $get_money;
	        $vmoney_data['remark'] = '被购买圈子商品获得V币'.$get_money;
	        $this->VmoneyTable->add($vmoney_data);
	        
	        $vlevel_data['uid'] = $circle_data['uid'];
	        $vlevel_data['type'] = 7;
	        $vlevel_data['level'] = $get_money;
	        $vlevel_data['remark'] = '被购买圈子商品获得V级';
	        $this->VlevelTable->add($vlevel_data);
	        
	        $msg_data['uid'] = $circle_data['uid'];
	        $msg_data['createdate'] = $date;
	        $msg_data['msg'] = '又来收入了！有人购买了您的圈子商品！';
	        $msg_data['status'] = 0;
	        $this->MsgTable->add($msg_data);
	        
	        $exreas = array(
	            'income' => '离土豪又近了一步哦！',
	            'pushtype' => 2,
	            'content' => '离土豪又近了一步哦！',
	            'customfield' => 'http://mp.weixin.qq.com/s/*888**502473ae@t,0*u,UserCentreCircleInMoneyController*888**'
	        );
	        
	        do_jpush($circle_data['uid'],'又来收入了',$exreas);
	        
	        
	        $microsecond = explode(' ', microtime()); //' '中间是一个空格
	        $ms = explode('.',$microsecond[0]);
	        $customfield = array(
	            'nickName' => "用户ID：{$order_data['uid']}",
	            'type' => '1',
	            'icon' => 'temporary11',
	            'messageSection' => $order_data['uid'],
	            'time' => $date,
	            'chatId' => $date.$ms[1]
	        );
	        $message_push = "用户{$order_data['uid']}：我在《小树林》APP购买了您的圈子商品\n{$circle_data['remark']}\n如果此圈子商品使用中需要什么注意事项请及时回复我\n如果没有及时处理用户的问题，被用户投诉后将扣除您收入的0.1元";
	        $exreas = array(
	            'income' => $message_push,
	            'pushtype' => 14,
	            'content' => $message_push,
	            'customfield' => json_encode($customfield)
	        );
	        do_jpush($circle_data['uid'],$message_push,$exreas);
	        
	    }elseif($order_data['type'] == 2){
	        $is_buy = $this->CirclebuytopTable->getRow("end_date > '{$date}' and cid = {$order_data['cid']} and status = 1",'id desc');
	        if(isset($is_buy['id']) && $is_buy['id'] > 0){
	            $start_date = $is_buy['end_date'];
	        }else{
	            $start_date = $date;
	        }
	        $end_date = date('Y-m-d H:i:s',strtotime($start_date)+$order_data['pay_day']*86400);
	        
	        $data = array(
	            'cid' => $order_data['cid'],
	            'uid' => $order_data['uid'],
	            'status' => 1,
	            'create_date' => $date,
	            'buyday' => $order_data['pay_day'],
	            'price' => $order_data['price'],
	            'start_date' => $start_date,
	            'end_date' => $end_date
	        );
	        
	        $this->CirclebuytopTable->add($data);
	        
	        $message = '圈子已置顶，会被更多人看到。';
	    }
	    
	    $title = mb_substr($circle_data['content'], 0, 90);
	    if($pay_type == 5){
	        echo json_encode(array('returncode'=>'0','message'=>$message,'newid'=>(string)$order_data['cid'],'orderid'=>(string)$order_data['orderid'],'price'=>(string)$order_data['price'],'title'=>$title,'paytype'=>'5'));
	        exit();
	    }else{
	        echo json_encode(array('returncode'=>'0','message'=>$message,'newid'=>(string)$order_data['cid'],'title'=>$title));
	        exit();
	    }
	}
	
	public function getremarkAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>'-1','message'=>'用户信息不正确','remark'=>''));
	        exit();
	    }
	    
	    $id = $this->getIntParam('circleid');
	    $circle_data = $this->CircleTable->getRow($id);
	    if(!isset($circle_data['id']) || $circle_data['id'] < 1 || $circle_data['status'] != 1){
	        echo json_encode(array('returncode'=>'-2','message'=>'无此圈子','remark'=>''));
	        exit();
	    }
	    
	    $order_data = $this->CircleorderTable->getRow(array(
	        'status' => 2,
	        'cid' => $circle_data['id'],
	        'uid' => $uid
	    ));
	    if(!isset($order_data['id'])){
	        echo json_encode(array('returncode'=>'-3','message'=>'尚未购买','remark'=>''));
	        exit();
	    }
	    
	    $time = time();
	    $timetoday = strtotime(date('Y-m-d'));
	    $new_v = $this->getCirclePrice($circle_data['fixation_date'],$timetoday,$circle_data['price'],$circle_data['assure_date']);
	    //$circle_data['price'] = $new_v['price'];
	    $circle_data['assuredate'] = $new_v['day'];
	    if($circle_data['assuredate'] < 1){
	        echo json_encode(array('returncode'=>'-4','message'=>'担保期已过','remark'=>''));
	        exit();
	    }
	    
	    $remark = $circle_data['remark'];
	    $from = $this->getParam('from');
	    if($from == 'sharecircle'){
	        $remark = "#小树林APP剩余担保期:{$circle_data['assuredate']}天##圈子ID:{$circle_data['id']}#\n{$remark}";
	    }
	    
	    echo json_encode(array('returncode'=>'0','message'=>'成功','remark'=>$remark));
	    exit();
	}
	
	public function getcircleAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>'-1'));
	        exit();
	    }
	    
	    $circleid = $this->getIntParam('circleid');
	    
	    $circle_data = $this->CircleTable->getRow($circleid);
	    if(!isset($circle_data['id']) || $circle_data['status'] == 0){
	        echo json_encode(array('returncode'=>'-1'));
	        exit();
	    }
	    
	    $user_data = $this->UserTable->getRow($circle_data['uid']);
	    
	    $tmp_like_uid = array();
	    $tmp_like_list = $this->CirclelikeTable->listAll(array('cid'=>$circle_data['id'],'status'=>1));
	    foreach ($tmp_like_list as $v2){
	        $tmp_like_uid[] = $v2['uid'];
	    }
	    
	    $tmp_comm_uid = array();
	    $tmp_comm_list = $this->CirclecommTable->listAll(array('cid'=>$circle_data['id'],'status'=>1));
	    foreach ($tmp_comm_list as $v2){
	        $tmp_comm_uid[] = $v2['uid'];
	    }
	    
	    if($circle_data['is_order'] == 1){
	        $time = time();
	        $timetoday = strtotime(date('Y-m-d'));
	        $new_v = $this->getCirclePrice($circle_data['fixation_date'],$timetoday,$circle_data['price'],$circle_data['assure_date']);
	        //$circle_data['price'] = $new_v['price'];
	        $circle_data['assuredate'] = $new_v['day'];
	    }elseif((int)$circle_data['is_fixation'] == 0){
	        $time = time();
	        $timetoday = strtotime(date('Y-m-d'));
	        $new_v = $this->getCirclePrice($circle_data['create_date'],$timetoday,$circle_data['price'],$circle_data['assure_date']);
	        $circle_data['price'] = $new_v['price'];
	        $circle_data['assuredate'] = $new_v['day'];
	    }
	    
	    $is_like = in_array($uid, $tmp_like_uid) ? 1 : 0;
	    $tmp_collect = $this->CirclecollectTable->getRowWithFields('id',array('cid'=>$circle_data['id'],'uid'=>$uid,'status'=>1));
	    $is_collect = isset($tmp_collect['id']) ? 1 : 0;
	    
	    $data = array(
	        'circleid' => (string)$circle_data['id'],
	        'circleuid' => (string)$circle_data['uid'],
	        'avatar' => (string)$user_data['headurl'],
	        'name' => ($circle_data['uid'] == 1374 ? '小树林官方APP' : '用户ID：'.$circle_data['uid']),
	        'content' => (string)$circle_data['content'],
	        'imgs' => array(),
	        'thumbnails' => array(),
	        'hyperlinkimage' => (string)$circle_data['link_img'],
	        'hyperlinkdescribe' => (string)$circle_data['link_content'],
	        'hyperlinkurl' => (string)$circle_data['link_url'],
	        'assuredate' => (string)$circle_data['assure_date'],
	        'price' => (string)$circle_data['price'],
	        'date' => (string)$circle_data['create_date'],
	        'iscollect' => (string)$is_collect,
	        'isstick' => (string)0,
	        'islike' => (string)$is_like,
	        'likelist' => $tmp_like_uid,
	        'commentlist' => $tmp_comm_uid,
	        'remark' => '',
	        'order_count' => 0,
	        'isfixation' => (string)$circle_data['is_fixation'],
	        'repertory' => (string)$circle_data['repertory']
	    );
	    
	    $img_data = $this->CircleimgTable->listAll(array('cid'=>$circle_data['id'],'status'=>1));
	    foreach ($img_data as $v){
	        $data['imgs'][] = $v['img_url'];
	        $data['thumbnails'][] = $v['img_url'];
	    }
	    
	    if($uid == $circle_data['uid']){
	        $data['remark'] = $circle_data['remark'];
	        $data['order_count'] = $this->CircleorderTable->listCount(array('old_cid'=>$circle_data['id'],'status'=>2));
	        $data['isfixation'] = $circle_data['is_fixation'];
	        $data['repertory'] = $circle_data['repertory'];
	    }
	    
	    echo json_encode(array('returncode'=>'0','circle'=>$data));
	    exit();
	}
	
	public function sharecircleAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>'-1','message'=>'用户信息不正确','remark'=>''));
	        exit();
	    }
	    
	    $shareid = $this->getIntParam('shareid');
	    $shareuid = $this->getIntParam('shareuid');
	    $sharetype = $this->getIntParam('sharetype');
	    
	    $sign1 = $shareid.$shareuid.$sharetype.'397zx.cASD!@#';
	    $sign2 = md5($sign1);
	    $sign3 = substr($sign2,3,16);
	    
	    $shareverify = $this->getParam('shareverify');
	    if($sign3 != $shareverify){
	        echo json_encode(array('returncode'=>'-2','message'=>'验证失败','remark'=>''));
	        exit();
	    }
	    
	    $data = array(
	        'uid' => $this->getIntParam('shareuid'),
	        'circleid' => $this->getIntParam('shareid'),
	        'testj' => 1,
	        'from' => 'sharecircle'
	    );
	    $this->redirectToAction('getremark',$data);
	}
	
	
	public function headnewsAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getIntParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>'-1','message'=>'用户信息不正确','data'=>array()));
	        exit();
	    }
	    
	    $where = array(
	        'status' => 1,
	        'type' => $machine_type
	    );
	    $data = $this->CircleheadnewsTable->listAll($where);
	    
	    $data_re = array();
	    foreach ($data as $v){
	        $data_re[] = array(
	            'describe' => $v['title'],
	            'url' => $v['link']
	        );
	    }
	    
	    echo json_encode(array('returncode'=>'0','message'=>'','data'=>$data_re));
	    exit();
	}
	
	public function inmoneyAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getIntParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>'-1','message'=>'用户信息不正确','data'=>array(),'money'=>'0'));
	        exit();
	    }
	    
	    $page = $this->getIntParam('page');
	    $page = $page < 1 ? 1 : $page;
	    $size = $this->getIntParam('size');
	    $size = $size < 1 ? 10 : $size;
	    $pv = ($page - 1) * $size;
	    
	    $data = array();
	    $money = 0;
	    
	    $db = $this->CircleTable->getDatabase();
	    $sql = "select a.orderid,a.price,b.content,a.cid from tb_circle_order as a , tb_circle as b where a.old_cid in (select id from tb_circle where uid = {$uid}) and a.status = 2 and a.cid = b.id order by b.id desc limit {$pv} , {$size}";
	    $db->query($sql);
	    $re = $db->getList();
	    if(is_array($re)){
	        foreach ($re as $v){
	            $data[] = array(
	                'orderid' => $v['orderid'],
	                'money' => $v['price'],
	                'content' => mb_substr($v['content'], 0, 90),
	                'cid'=>$v['cid']
	            );
	        }
	    }
	    
	    $sql = "select SUM(price) as sprice from tb_circle_order where old_cid in (select id from tb_circle where uid = {$uid}) and status = 2 ";
	    $db->query($sql);
	    $re = $db->getList();
	    if(is_array($re)){
	        foreach ($re as $v){
	            $money = $v['sprice'];
	        }
	    }
	    
	    
	    echo json_encode(array('returncode'=>'0','message'=>'','data'=>$data,'money'=>(string)$money));
	    exit();
	}
	
	protected function out()
	{
		$this->layout->nav		= 'index';
		parent::out();
	}
}

Config::extend('CircleController', 'Controller');
